package com.study.springcloudgateway.routePredicateFactory;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @description：权限断言类扩展
 * @author：李军荣
 * @date: 2020/12/20 22:31
 */
@Component
public class AuthRoutePredicateFactory extends AbstractRoutePredicateFactory<AuthRoutePredicateFactory.Config> {

    public AuthRoutePredicateFactory() {
        super(AuthRoutePredicateFactory.Config.class);
    }

    public static final String NAME_KEY = "name";


    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(NAME_KEY);
    }


    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return (exchange) ->{
            List<String>  headers = exchange.getRequest().getHeaders().get(config.name);
            return headers != null &&  headers.size() > 0;
        };
    }



    public static class Config {

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}
